package com.google.gwt.sample.dynatablerf.client.user.presenters;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.sample.dynatablerf.client.user.events.GoToCategoriaViewEvent;
import com.google.gwt.sample.dynatablerf.client.user.events.GoToProdutoListViewEvent;
import com.google.gwt.sample.dynatablerf.shared.AppRequestFactory;
import com.google.gwt.sample.dynatablerf.shared.AppRequestFactory.CategoriaRequest;
import com.google.gwt.sample.dynatablerf.shared.CategoriaProxy;
import com.google.gwt.sample.dynatablerf.shared.CategoriaQueryParamsProxy;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.googlecode.mgwt.dom.client.event.touch.TouchEndEvent;
import com.googlecode.mgwt.dom.client.event.touch.TouchEndHandler;
import com.googlecode.mgwt.ui.client.widget.base.ButtonBase;

public class CategoriaListPresenter {

	public interface CategoriaDisplay extends IsWidget {

		void copyFrom(List<ButtonBase> buttonList);

		ButtonBase createButton();
	}

	private EventBus eventBus;
	private CategoriaDisplay categoriaDisplay;
	private AppRequestFactory appRequestFactory;
	private CategoriaProxy parent;
	private HasWidgets container;

	public CategoriaListPresenter(CategoriaProxy parent,
			CategoriaDisplay mainDisplay, EventBus eventBus,
			AppRequestFactory appRequestFactory) {
		super();
		this.parent = parent;
		this.categoriaDisplay = mainDisplay;
		this.appRequestFactory = appRequestFactory;
		this.eventBus = eventBus;
	}

	public void go(final HasWidgets hasWidgets) {
		fetch();
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {

			@Override
			public void execute() {
				container = hasWidgets;
				hasWidgets.clear();
				hasWidgets.add(categoriaDisplay.asWidget());

			}
		});
	}

	public CategoriaDisplay getMainDisplay() {
		return categoriaDisplay;
	}

	private void fetch() {
		CategoriaRequest categoriaRequest = appRequestFactory
				.categoriaRequest();

		CategoriaQueryParamsProxy paramsProxy = categoriaRequest
				.create(CategoriaQueryParamsProxy.class);
		paramsProxy.setParent(parent);

		categoriaRequest.findAll(paramsProxy, 0, 0).with("produtoSet")
				.fire(new Receiver<List<CategoriaProxy>>() {

					@Override
					public void onSuccess(List<CategoriaProxy> response) {
						categoriaDisplay
								.copyFrom(getCategoriaProxyButtons(response));

					}
				});

	}

	private List<ButtonBase> getCategoriaProxyButtons(
			List<CategoriaProxy> categoriaProxyList) {
		List<ButtonBase> buttonBaseList = new ArrayList<ButtonBase>();
		if (categoriaProxyList != null) {
			for (final CategoriaProxy categoriaProxy : categoriaProxyList) {
				ButtonBase buttonBase = categoriaDisplay.createButton();
				buttonBase.setText(categoriaProxy.getNome());
				buttonBase.addTouchEndHandler(new TouchEndHandler() {

					@Override
					public void onTouchEnd(TouchEndEvent event) {
						if (!categoriaProxy.getProdutoSet().isEmpty())
							eventBus.fireEvent(new GoToProdutoListViewEvent(
									container, categoriaProxy));

						else
							eventBus.fireEvent(new GoToCategoriaViewEvent(
									container, categoriaProxy));

					}
				});
				buttonBaseList.add(buttonBase);
			}
		}
		return buttonBaseList;
	}
}